package scau.mis4.chanyulin.service;

import scau.mis4.chanyulin.entity.Temp;
import java.sql.Timestamp;
import java.util.Random;

/**
 * Created by cyl on 2017/11/26.
 * 温度获取工具类
 */
public class DataSourceService {

    // 临时保存上一次得到的温度
    public static double lastTemper = 23.5;

    /**
     * 获取一个随机的温度
     * 范围：20.0 ~ 29.9
     * @return
     */
    public static double randomTemper() {
        int largeChange = new Random().nextInt(100);
        if (largeChange <= 5) {
            Random random = new Random();
            int s1 = random.nextInt(10) % 10;
            int s2 = random.nextInt(10) % 10;
            lastTemper = 20 + s1 + s2 / 10.0;
            return lastTemper;
        }
        int s = new Random().nextInt(10) % 10;
        int plusOrDiv = new Random().nextInt(2) % 10;
        if (plusOrDiv == 0)
            lastTemper = lastTemper + s / 10.0;
        else
            lastTemper = lastTemper - s / 10.0;
        return lastTemper;
    }

    /**
     * 获取一个随机的温度对象
     * 范围：20.0 ~ 29.9
     * @return
     */
    public static Temp randomTemperObject() {
        int largeChange = new Random().nextInt(100);
        if (largeChange <= 5) {
            Random random = new Random();
            int s1 = random.nextInt(10) % 10;
            int s2 = random.nextInt(10) % 10;
            lastTemper = 20 + s1 + s2 / 10.0;
            return new Temp(0, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, new Timestamp(System.currentTimeMillis()));
        }
        int s = new Random().nextInt(10) % 10;
        int plusOrDiv = new Random().nextInt(2) % 10;
        if (plusOrDiv == 0)
            lastTemper = lastTemper + s / 10.0;
        else
            lastTemper = lastTemper - s / 10.0;
        return new Temp(0, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, lastTemper, new Timestamp(System.currentTimeMillis()));
    }

}
